package mblog.web.controller.desk.tuiguang;

import mblog.base.utils.GeoIps;
import mblog.base.utils.IpAdrressUtil;
import mblog.base.utils.ObjectUtils;
import mblog.base.utils.SnowflakeIdWorker;
import mblog.core.data.Addresses;
import mblog.core.persist.service.AddressesService;
import mblog.core.persist.service.RedisService;
import mblog.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 *
 */

@Controller
@RequestMapping("/fc")
public class FangchanController extends BaseController {
    @Autowired
    private AddressesService addressesService;

    @Autowired
    private RedisService redisService;

    public void initAddress(ModelMap model, String province, String city, String location,HttpServletRequest request){

        String realIp = IpAdrressUtil.getIpAdrress(request);
        Map<String,String> nameMap = new HashMap<>();
        try {
            nameMap = GeoIps.getLocationInfoByIp(realIp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<Addresses> dataList = addressesService.findByParentId("1");
        if(nameMap.containsKey("subdivision")){
            for(Addresses data : dataList){
                if(data.getName().contains(nameMap.get("subdivision"))){
                    model.put("province",data.getId());
                    province = String.valueOf(data.getId());
                    break;
                }
            }
        }
        List<Addresses> twoList = new ArrayList<>();
        if(StringUtils.isNotEmpty(province)){
            twoList = addressesService.findByParentId(province);
        }else{
            twoList = addressesService.findByParentId(String.valueOf(dataList.get(0).getId()));
        }
        if(nameMap.containsKey("city")){
            for(Addresses data : twoList){
                if(data.getName().contains(nameMap.get("city"))){
                    model.put("city",data.getId());
                    break;
                }
            }
        }


        List<Addresses> threeList = new ArrayList<>();
        if(StringUtils.isNotEmpty(city)){
            threeList = addressesService.findByParentId(city);
        }else{
            threeList = addressesService.findByParentId(String.valueOf(twoList.get(0).getId()));
        }

        model.put("cityList", dataList);
        model.put("twoList", twoList);
        model.put("threeList", threeList);
    }

    @RequestMapping("/index")
    public String index(ModelMap model,HttpServletRequest request) {
        String sessionId = session.getId();
        initAddress(model,null,null,null,request);
        model.put("showId",sessionId);
        return "/wx/tuiguang/fangchan";
    }

    @RequestMapping("/ajaxData")
    @ResponseBody
    public String ajaxData(String showId,String a,String b,String c,String d,ModelMap model,HttpServletRequest request) {
        FangChanDto fangChanDto = new FangChanDto();
        fangChanDto.setA(a);
        fangChanDto.setB(b);
        fangChanDto.setC(c);
        fangChanDto.setD(d);

        if(redisService.exists(showId)){
            redisService.remove(showId);
        }
        System.out.println("ajaxData"+showId);
        redisService.set(showId,fangChanDto);
        return d;
    }

    @RequestMapping("/next")
    public String next(ModelMap model,String showId,HttpServletRequest request) {
        System.out.println("next"+showId);
        if(redisService.exists(showId)){
            FangChanDto fangChanDto = (FangChanDto)redisService.get(showId);
            model.put("fName",fangChanDto.getD());
        }
        model.put("showId",showId);
        return "/wx/tuiguang/next";
    }

    @RequestMapping(value = "/show",method = RequestMethod.POST)
    @ResponseBody
    public String show(ModelMap model,String showId,Integer e,Integer f,Integer g,Integer h,Integer i,HttpServletRequest request) {
//        String showId = session.getId();
        if(redisService.exists(showId)){
            FangChanDto fangChanDto = (FangChanDto)redisService.get(showId);
            redisService.remove(showId);

            fangChanDto.setE(e);
            fangChanDto.setF(f);
            fangChanDto.setG(g);
            fangChanDto.setH(h);
            fangChanDto.setI(i);

            redisService.set(showId,fangChanDto);

            model.put("fangChanDto",fangChanDto);
        };
        System.out.println("showId"+showId);
        return "/wx/tuiguang/show";
    }

    @RequestMapping(value = "/show/{showId}",method = RequestMethod.GET)
    public String show(ModelMap model,@PathVariable String showId,HttpServletRequest request) {
        System.out.println("showId"+showId);
        if(redisService.exists(showId)){
            FangChanDto fangChanDto = (FangChanDto)redisService.get(showId);
            model.put("fangChanDto",fangChanDto);
        };
        System.out.println("showId"+showId);

//        Jedis jedis = new Jedis("localhost");
//        System.out.println("连接成功");
//        //获取数据并输出
//        Set<String> keys = jedis.keys("*");
//        Iterator<String> it=keys.iterator() ;
//        while(it.hasNext()){
//            String key = it.next();
//            System.out.println(key);
//        }

        model.put("showId",showId);
        return "/wx/tuiguang/show";
    }



    //    @RequestMapping("/create")
//    @ResponseBody
//    public Long create(String a,String b,String c,String d,String f,ModelMap model,HttpServletRequest request) {
//        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
//        Long nextId = idWorker.nextId();
//
//        String path = "/Users/mark/IdeaProjects/"+nextId+"/"+nextId+".txt";
//        File file = new File(path);
//        if(!file.exists()){
//            file.getParentFile().mkdirs();
//        }
//        try {
//            file.createNewFile();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        // write
//        FileWriter fw = null;
//        try {
//            fw = new FileWriter(file, true);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        BufferedWriter bw = new BufferedWriter(fw);
//        try {
//            bw.write("xxxaffdf");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            bw.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            bw.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            fw.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return nextId;
//    }


}
